/**
 * Copyright (c) 2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.composedui;

import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;

import com.nokia.mid.ui.orientation.Orientation;
import com.nokia.mid.ui.orientation.OrientationListener;

import com.nokia.example.composedui.model.ItemStorage;
import com.nokia.example.composedui.utils.AppDataStorage;
import com.nokia.example.composedui.views.GridView;
import com.nokia.example.composedui.views.ListView;
import com.nokia.example.composedui.views.SplitView;

/**
 * The main class of the application.
 */
public class Main
    extends MIDlet
    implements OrientationListener
{
    // Members
    private ItemStorage itemStorage;
    private AppDataStorage appDataStorage;
    private TabManager tabManager;
    private boolean doSaveState;

    /**
     * Creates and shows the last visited view or the initial list view in case
     * no restoration point was available. This method also setups listeners and
     * enables the orientation support.
     * 
     * @see javax.microedition.midlet.MIDlet#startApp()
     */
    public void startApp() {
        appDataStorage = AppDataStorage.getInstance(this);
        appDataStorage.load();
        
        itemStorage = new ItemStorage(appDataStorage);
        tabManager = new TabManager(this);
        doSaveState = true;
        
        boolean restoreSucceeded = tabManager.restoreViewsFromAppDataStorage(itemStorage);
        
        if (restoreSucceeded == false) {
            Displayable view1 = new SplitView(itemStorage, tabManager);
            Displayable view2 = new ListView(itemStorage, tabManager);
            Displayable view3 = new GridView(itemStorage, tabManager);
            
            tabManager.addTab(view1, "/split-view-icon.png", "Custom command label");
            tabManager.addTab(view2, "/list-view-icon.png");
            tabManager.addTab(view3, "/grid-view-icon.png");
            tabManager.showTab();
        }
        
        Orientation.addOrientationListener(this);
    }

    /**
     * @see javax.microedition.midlet.MIDlet#pauseApp()
     */
    public void pauseApp() {
    }

    /**
     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
     */
    public void destroyApp(boolean unconditional) {
        if (doSaveState) {
            // Uncomment the following method call to enable the feel of 
            // multitasking; resuming the application takes you to the same
            // state in which the app was previously. This, however, violates
            // the current user experience design guidelines.
            //saveApplicationState();
        }
    }

    /**
     * Changes display orientation between landscape and portrait.
     * Note: ORIENTATION_LANDSCAPE_180 and ORIENTATION_PORTRAIT_180 are not
     * supported.
     * 
     * @param newDisplayOrientation Tells the new device orientation.
     * @see com.nokia.mid.ui.orientation.OrientationListener#displayOrientationChanged(int)
     */
    public void displayOrientationChanged(int newDisplayOrientation) {
        if (newDisplayOrientation == Orientation.ORIENTATION_LANDSCAPE ||
            newDisplayOrientation == Orientation.ORIENTATION_PORTRAIT)
        {
            Orientation.setAppOrientation(newDisplayOrientation);
        }
    }

    /**
     * Closes the application.
     */
    public void closeApp(boolean saveState) {
        doSaveState = saveState;
        notifyDestroyed();
    }

    /**
     * Saves and stores the application data. This method also saves the
     * model content since all the records are purged before saving anything.
     */
    protected void saveApplicationState() {
        appDataStorage.deletePermanently(); // Delete the old app state and model content data
        itemStorage.saveItemsToAppDataStorage(false); // Save the model content
        tabManager.saveViewsToAppDataStorage(); // Save the application state
        appDataStorage.save(false);
    }
}
